import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'

import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,parseToChart} from '../apis/core-util'
import basicInfo from '../apis/basicInfo'
import {tracelistApi,createGridOrder,parseToPartGrid} from '../apis/tracelist'
export default class MainScope extends Scope {
  constructor(props){
    super(props);
    this.nameId = props.routeParams.interactionId;
    this.$state = {
      chart0:{
        loading:true
      },
      chart1:{
        loading:true
      },
      pie0:{
        loading:true
      },
      pie1:{
        loading:true
      },
      grid:{
        loading:true,
        getOrder:createGridOrder('timestamp',-1),
        fetch:this.fetchGrid.bind(this)
      },
      info:null
    }
    this.basicInfo = basicInfo({
      id:this.nameId,
      urlName:'interaction'
    })
    this.chart0Api = coreApi({
      limit:5,
      mode:'ui',
      nameId:this.nameId,
      classify:'measurementname',
      urlName:'interactionDetail',
      viewKey:'avg_exec'
    },TimeStore.getValue,function (){
      return {order:sortMap[this.viewKey]}
    });
    this.chart1Api = coreApi({
      limit:5,
      mode:'worker',
      nameId:this.nameId,
      classify:'measurementname',
      urlName:'interactionDetail',
      viewKey:'exec_count'
    },TimeStore.getValue,function (){
      return {order:sortMap[this.viewKey]}
    });
    this.pie0Api = coreApi({
      urlName:'activitytracesimilar',
      limit:200,
      size:1,
      classify:'appversion',
      viewKey:'activity_similar_trace_count',
      nameId:this.nameId
    },TimeStore.getValue);
    this.pie1Api = coreApi({
      urlName:'activitytracesimilar',
      limit:200,
      size:1,
      classify:'osversion',
      viewKey:'activity_similar_trace_count',
      nameId:this.nameId
    },TimeStore.getValue);
    this.gridApi = tracelistApi({
      urlName:'activitytracelist',
      nameId:this.nameId
    },TimeStore.getValue,this.$state.grid.getOrder);
    this.traceInfoApi = coreApi({
      urlName:'activitytracesimilar',
      limit:200,
      size:1,
      classify:'measurementname',
      nameId:this.nameId
    },TimeStore.getValue);
  }
  fetch(){
    var _this = this
    this.chart0Api.fetch().then(function(data,params){
      _this.$state.chart0 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart0 = {
        error:err
      }
    },function(params){
      _this.$state.chart0 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart1Api.fetch().then(function(data,params){
      _this.$state.chart1 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart1 = {
        error:err
      }
    },function(params){
      _this.$state.chart1 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.traceInfoApi.fetch().then(function(data,params){
      if(data.result.length){
        _this.$state.info = _.extend({},data.result[0].combData,data.result[0].key)
      }
    }).always(function(){
      _this.emit()
    })
    this.pie0Api.fetch().then(function(data,params){
      _this.$state.pie0 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.pie0 = {
        error:err
      }
    },function(params){
      _this.$state.pie0 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.pie1Api.fetch().then(function(data,params){
      _this.$state.pie1 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.pie1 = {
        error:err
      }
    },function(params){
      _this.$state.pie1 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.fetchGrid()
  }
  fetchGrid(){
    var _this = this
    this.gridApi.fetch().then(function gridHandle(data,params){
      _this.$state.grid = parseToPartGrid(data,params)
    },function(err){
      _this.$state.grid = {
        error:err
      }
    },function(params){
      _this.$state.grid = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
  }
  $settime(payload){
    this.fetch()
  }
  $rename_interaction(payload){
    this.$state.title = payload.name
    this.emit()
  }
  $unload(){
  }
  $load(){
    var _this = this
    this.basicInfo.fetch().done(function(data){
      //TODO 当basicInfo查不到时应显示错误,可以在Router.transtition时加载basicInfo并判断
      _this.$state.title = data.result[0].alias||data.result[0].name
      _this.emit()
    })
    this.fetch();

  }
}
